﻿/**
HCRain    追雨潮开发框架   hcrain.cn   
追雨潮.net 工作流引擎js处理  @追雨潮
**/
//var spPageUrl = "../ashx/spui.ashx";//后台统一页面
//var spPageHtmlName = "本html名称";//如index
//var pTableName = "";//增删改的，表名
//var pTableIdName = "";//增删改的，表主键
//var grid1ID = "";//列表的ID
//var selTableinfo = "";//查询table信息 主表一定要as T 
//var selFieldsinfo = "*";//查询返回的内容
//var pPageCode = "";
//var pPageInfoName = "-";

//var auhcotype = "";
//var Hcaumid = "";
var hcrainwfurl = "../ashx/spui.ashx";
////业务ID 
//var Hcaumid = "";
//业务代码(已有)
//var pPageCode = "";
//关闭后，跳转的页面
var urlpcend = "";
var Hcusertitels;
function hsinfo(info) {
    layer.alert(info, { icon: 2 });
}
function hsmsg(info) {
    layer.msg(info, { shift: 6 });
}


//   sep_id   sep_de_id  sep_name  sep_type
var hcrnid, hcrdeid, hcrnname, hcrntype, fdoonlyuser;
//初始化工作流功能栏
function hcrfbutton() {
    Hcusertitels = HCUser.HCUtitle;
    //流程步骤名称
    var setupname = "发起-待发起";
    var divshow = $("#divforhcrfbutton");
    divshow.text("");// 清空数据
    //if (Hcaumid == null || Hcaumid == "") {
    //    //没有处理——————查找发起的权限
    //    divshow.append("工作流操作-当前步骤:<strong style=\"font-weight: bold; color: #FF0000; padding-top: 1px;\">" + setupname + "</strong><br/>");
    //    divshow.append("意见:<textarea id=\"hcrf_fdesc\" name=\"hcrf_fdesc\"   class=\"textarea\" style=\"width: 500px\"></textarea><br/>");
    //    divshow.append("<a onclick=\"hcrf_start()\"  class=\"btn btn-primary radius\"   id=\"hcrain_bt_start\" >提交</a>&nbsp;");
    //}
    //else
    {
        //查找流程状态
        $.ajax({
            type: "post",
            url: hcrainwfurl,
            data: {
                "action": "hcrf_getyw",
                "wycode": pPageCode,
                "midvalue": Hcaumid,
                "infotype": "1"

            },
            timeout: 9000,
            async: true,
            cache: false,
            error: function (error) {
                hsmsg(error);
            },
            success: function (data) {
                var result = JSON.parse(data);
                switch (result.message) {
                    case JsonResult.Success:
                        {

                            var datainfo = result.data[0];
                            hcrnid = datainfo.hcrnid;
                            hcrdeid = datainfo.hcrdeid;
                            hcrnname = datainfo.hcrnname;
                            hcrntype = datainfo.hcrntype;
                            fdoonlyuser = datainfo.fdoonlyuser;
                            setupname = datainfo.hcrnname + "(" + datainfo.hcrntype + ")" + "-" + datainfo.hcrywstate;
                            //
                            $("#lablehcrnname").html(datainfo.hcrnname+"");
                            divshow.append("工作流操作-当前步骤:<strong style=\"font-weight: bold; color: #FF0000; padding-top: 1px;\">" + setupname + "</strong><br/><br/>");


                            if (datainfo.hcrntype == "结束") {
                                divshow.append("*已完成*");
                                //是否可以删除
                                if (datainfo.fcandel == "是") {
                                    divshow.append("<br/><br/><a onclick=\"hcwfdelete()\"   class=\"btn btn-danger radius\"  id=\"hcrain_bt_start\" >删除记录</a>&nbsp;");
                                }
                                $("#btn_hcrwf_save").hide();
                                return;
                            }
                            var ischeckok = false;
                            if (fdoonlyuser != "否") {

                                if (datainfo.hasstart=="0") { }//没提交状态-不处理,由角色处理
                                else {
                                    if (fdoonlyuser == "创建人") {
                                        if (HCUser.HCUide == datainfo.fywcreuid) { ischeckok = true; }
                                        else {
                                            $("#btn_hcrwf_save").hide();
                                            divshow.append("*没有权限*由创建人处理");
                                            return;
                                        }
                                    }
                                    else if (fdoonlyuser == "提交人") {
                                        if (HCUser.HCUide == datainfo.fywsubuid) { ischeckok = true; }
                                        else {
                                            $("#btn_hcrwf_save").hide();
                                            divshow.append("*没有权限*提交人");
                                            return;
                                        }
                                    }

                                }

                            }
                         
                            if (ischeckok == false) {
                                if (checkhcwfacess(Hcusertitels, datainfo.hcrdotitle)) {//access can do check ,now only checkby titles
                                    // [hcrdodep], [hcrdoorg], [hcrdouser], [hcrdotitle] 
                                }
                                else {
                                    $("#btn_hcrwf_save").hide();
                                    divshow.append("*没有权限*由" + datainfo.hcrdotitle + "处理");
                                    return;
                                }
                            }


                            if (datainfo.hcrcansave == "是") {
                                $("#btn_hcrwf_save").show();
                            }
                            else { $("#btn_hcrwf_save").hide(); }

                            divshow.append("<textarea id=\"hcrf_fdesc\" name=\"hcrf_fdesc\"   placeholder=\"提交备注\" class=\"textarea\" style=\"width: 500px;height:50px;\" ></textarea><br/><br/>");
                            if (datainfo.hasstart == "0") {
                                divshow.append("<a onclick=\"hcrf_start()\"   class=\"btn btn-primary radius\"  id=\"hcrain_bt_start\" >提交</a>&nbsp;");
                            }
                            else {



                                if (datainfo.hcrntype == "审核") {
                                    divshow.append("<a onclick=\"hcrf_che_ok('通过')\"   class=\"btn btn-success radius\"  id=\"hcrain_bt_ok\" >通过</a>&nbsp;");
                                    divshow.append("<a onclick=\"hcrf_che_no('退回')\"  class=\"btn btn-warning radius\"  id=\"hcrain_bt_no\" >退回</a>&nbsp;");
                                }
                                else if (datainfo.hcrntype == "处理") {
                                    divshow.append("<a onclick=\"hcrf_che_ok('已解决')\"   class=\"btn btn-success radius\"  id=\"hcrain_bt_ok\" >已解决</a>&nbsp;");
                                    divshow.append("<a onclick=\"hcrf_che_ok('没解决')\"   class=\"btn btn-secondary radius\"  id=\"hcrain_bt_ok1\" >没解决</a>&nbsp;");
                                    divshow.append("<a onclick=\"hcrf_che_no('退回')\"  class=\"btn btn-warning radius\"  id=\"hcrain_bt_no\" >退回</a>&nbsp;");
                                }
                                else if (datainfo.hcrntype == "发起") {
                                    divshow.append("<a onclick=\"hcrf_start()\"  class=\"easyui-linkbutton\"  id=\"hcrain_bt_start\" >提*交</a>&nbsp;");
                                }
                                else if (datainfo.hcrntype == "结束") {
                                    divshow.append("*已完成*");

                                }
                                else {
                                    divshow.append("*待系统处理*");
                                }
                                //是否可以删除
                                if (datainfo.fcandel == "是") {
                                    divshow.append("<br/><br/><a onclick=\"hcwfdelete()\"   class=\"btn btn-danger radius\"  id=\"hcrain_bt_start\" >删除记录</a>&nbsp;");
                                }

                            }
                        }
                        break;
                    default:
                        break;
                }

            }
        });
        //
        hcrain_wflogshow();
    }
}



//发起流程
function hcrf_start() {

    if (Hcaumid == null || Hcaumid == "") {
        hsinfo("业务没保存，不能发起");
        return;
    }
    if (pPageCode == null || pPageCode == "") {
        hsinfo("业务代码为空，不能发起");
        return;
    }
    hcrain_bt_nou();
    $.ajax({
        type: "post",
        url: hcrainwfurl,
        data: {
            "action": "hcrf_start",
            "fruntype": pPageCode,
            "frunywid": Hcaumid,
            "fhcrdostate": "发起",
            "fdesc": "发起业务"
        },
        timeout: 9000,
        async: true,
        cache: false,
        error: function (error) {
            hsmsg(error);
            hcrain_bt_oku();
        },
        success: function (data) {
            var result = JSON.parse(data);
            switch (result.message) {
                case JsonResult.Success:
                    hsmsg("提交成功");
                    layer_close();
                    // location.href = urlpcend;
                    return;
                    break;
                case JsonResult.Error:
                    if (result.data.indexOf("index_w_runti") > 0) {
                        hsmsg("不需要重复发起!");
                    }
                    else
                        hsmsg(result.data);
                    hcrain_bt_oku();
                    break;
                default:
                    break;
            }

        }
    });


}
//审核通过
function hcrf_che_ok(dostate) {
    if (Hcaumid == null || Hcaumid == "") {
        hsinfo("业务没保存，不能处理");
        return;
    }
    if (pPageCode == null || pPageCode == "") {
        hsinfo("业务代码为空，不能处理");
        return;
    }
    hcrain_bt_nou();
    layer.confirm('确定提交到下一步？', {
        btn: ['确定', '否'],
        shade: false
    },
	function () {
	    $.ajax({
	        type: "post",
	        url: hcrainwfurl,
	        data: {
	            "action": "hcrf_next",
	            "fruntype": pPageCode,
	            "frunywid": Hcaumid,
	            "fhcrdostate": dostate,
	            "fdesc": $("#hcrf_fdesc").val(),
	            "fdoend": "通过",
	            "frunid": hcrnid,
	            "fdeid": hcrdeid,
	            "fnname": hcrnname,
	            "fntype": hcrntype

	        },
	        timeout: 9000,
	        async: true,
	        cache: false,
	        error: function (error) {
	            hsmsg(error);
	            hcrain_bt_oku();
	        },
	        success: function (data) {
	            var result = JSON.parse(data);
	            switch (result.message) {
	                case JsonResult.Success:
	                    hsmsg("提交成功");
	                    layer_close();
	                    return;
	                case JsonResult.Error:

	                    hsmsg(result.data);
	                    break;
	                    hcrain_bt_oku();
	                default:
	                    break;
	            }

	        }
	    });

	},
    function () { hcrain_bt_oku(); });

}
//审核不通过
function hcrf_che_no(dostate) {
    if (Hcaumid == null || Hcaumid == "") {
        hsinfo("业务没保存，不能处理");
        return;
    }
    if (pPageCode == null || pPageCode == "") {
        hsinfo("业务代码为空，不能处理");
        return;
    }
    hcrain_bt_nou();
    layer.confirm('确定打回到上一步？', {
        btn: ['确定', '否'],
        shade: false
    },
   function () {
       $.ajax({
           type: "post",
           url: hcrainwfurl,
           data: {
               "action": "hcrf_next",
               "fruntype": pPageCode,
               "frunywid": Hcaumid,
               "fhcrdostate": dostate,
               "fdesc": $("#hcrf_fdesc").val(),
               "fdoend": "拒绝",
               "frunid": hcrnid,
               "fdeid": hcrdeid,
               "fnname": hcrnname,
               "fntype": hcrntype
           },
           timeout: 9000,
           async: true,
           cache: false,
           error: function (error) {
               hsmsg(error);
               hcrain_bt_oku();
           },
           success: function (data) {
               var result = JSON.parse(data);
               switch (result.message) {
                   case JsonResult.Success:
                       layer_close();
                       return;
                   case JsonResult.Error:
                       hsmsg(result.data);
                       hcrain_bt_oku();
                       break;
                   default:
                       break;
               }

           }
       });
   }, function () { hcrain_bt_oku(); });
}

function hcwfdelete() {

    ID = Hcaumid;

    layer.confirm("确认要删除吗？", function (index) {
        $.get(spdelUrl(ID),
         function (data) {
             var data = JSON.parse(data);
             switch (data.message) {
                 case JsonResult.SessionOut:
                     layer.msg('登录超时，请重新登录!', { icon: 2, time: 1000 });
                     break;
                 case JsonResult.Error:
                     layer.msg('系统出错!' + data.data, { icon: 2, time: 1000 });
                     break;
                 case JsonResult.Success:
                     layer.msg('已删除!', { icon: 1, time: 1000 });
                     layer_close();
                     break;
             }
         });

    });
}
function hcrain_bt_oku() {


    if ($('#hcrain_bt_start'))
        $('#hcrain_bt_start').removeAttr("disabled");
    if ($('#hcrain_bt_ok'))
        $('#hcrain_bt_ok').removeAttr("disabled");
    if ($('#hcrain_bt_ok1'))
        $('#hcrain_bt_ok1').removeAttr("disabled");

    if ($('#hcrain_bt_no'))
        $('#hcrain_bt_no').removeAttr("disabled");
}
function hcrain_bt_nou() {
    if ($('#hcrain_bt_start'))
        $('#hcrain_bt_start').attr('disabled', "true");
    if ($('#hcrain_bt_ok'))
        $('#hcrain_bt_ok').attr('disabled', "true");
    if ($('#hcrain_bt_ok1'))
        $('#hcrain_bt_ok1').attr('disabled', "true");
    if ($('#hcrain_bt_no'))
        $('#hcrain_bt_no').attr('disabled', "true");


}
//工作流列表查询
function spselUrl_hf(spadd) {
    if (spadd != null && spadd != "undefined" && spadd.length > 0)
    { return hcrainwfurl + "?action=selhf&wycode=" + pPageCode + "&tableinfo=" + escape(selTableinfo) + "&refields=" + escape(selFieldsinfo) + "&wheresp= " + spadd; }
    else
        return hcrainwfurl + "?action=selhf&wycode=" + pPageCode + "&tableinfo=" + escape(selTableinfo) + "&refields=" + escape(selFieldsinfo) + "";
}
//工作流过程日志显示
function hcrain_wflogshow() {
    var divshow = $("#divforhcrainwflog");
    if (divshow == null || divshow.length < 1) return;
    divshow.text("");// 清空数据
    hcdosel_t("fnname,fntype,fdousername,fdesc,fdoend,fsubtime,fruningdo",
        "hcr_w_runuser",
        " fruntype='" + pPageCode + "' and frunywid='" + Hcaumid + "' ",
        "",
        "",
        " id desc ",
            function (data) {
                if (data == "") return;
                var objData = JSON.parse(data);
                for (var i = 0; i < objData.length; i++) {
                    divshow.append("<label>" +
                        " [执行步骤]：" +
                         objData[i].fnname + "-" +
                               objData[i].fntype + "[结果]：" +
                          objData[i].fdoend + "------" + objData[i].fdousername + " 于:" +
                        objData[i].fsubtime + " 备注：" +
                              objData[i].fdesc +
                        " </label><br />");

                }
            });
}
//能用查询 select table where groupby having order callback
function hcdosel_t(s, t, w, g, h, o, callback) {
    $.get(sposelUrlByFNoState_t(s, t, w, g, h, o) + "&timenow=" + new Date().getTime(),
            function (data) {
                callback(data);
            });
}
function sposelUrlByFNoState_t(s, t, w, g, h, o) {

    var re = "../ashx/sp.ashx" + "?action=SqlTableGet&nostate=no&sqlinfo=" + escape("select[" + s + "]table[" + t + "]where[" + w + "]group[" + g + "]having[" + h + "]order[" + o + "]");
    re = re.replaceAll("'", "hcdhc");
    return re;
}


function checkhcwfacess(userinfo, sysdeinfo) {
    if (sysdeinfo == null || sysdeinfo == "")
        return true;
    if (userinfo == null || userinfo == "")
        return false;
    var re = false;
    var replaceStr = "'";
    sysdeinfo = sysdeinfo.replace(new RegExp(replaceStr, 'gm'), '');
    var sysarray = sysdeinfo.split(",");
    $.each(sysarray, function (i, val) {
        if (val != null && val != "") {
            if (userinfo.indexOf(val) >= 0) {
                re = true;
                return false;
            }
        }
    });
    return re;
}